#include<iostream>
#include<string>
#include<websocketpp/server.hpp>
#include<websocketpp/config/asio_no_tls.hpp>
typedef websocketpp::server<websocketpp::config::asio> wsserver_t;
void http_callback(wsserver_t* srv,websocketpp::connection_hdl hdl)
{
    wsserver_t::connection_ptr conn = srv->get_con_from_hdl(hdl);
    std::cout<<conn->get_request_body()<<std::endl;
    websocketpp::http::parser::request req = conn->get_request();
    std::cout<<req.get_method()<<std::endl;
    std::cout<<req.get_uri()<<std::endl;

    std::string body;
    body="<html><body><h1>aaaaaaaaaaaaaaani chenggon le yo<h1/><body/><html/>";
    conn->set_body(body);
    conn->append_header("Content-Type","text/html");

    conn->set_status(websocketpp::http::status_code::ok);
}
void wsopen_callback(wsserver_t* srv,websocketpp::connection_hdl hdl)
{
    std::cout<<"websocket 握手成功"<<std::endl;
}
void wsclose_callback(wsserver_t* srv,websocketpp::connection_hdl hdl)
{
    std::cout<<"websocket 断开"<<std::endl;
}
void wsmsg_callback(wsserver_t* srv,websocketpp::connection_hdl hdl,wsserver_t::message_ptr msg)
{
    std::cout<<"wsmsg: "<<msg->get_payload()<<std::endl;
    std::string resp;
    resp="client say: "+ msg->get_payload();
    std::cout<<resp<<std::endl;
    wsserver_t::connection_ptr conn = srv->get_con_from_hdl(hdl);
    conn->send(resp);
}
int main()
{
    wsserver_t wssrv;

    wssrv.set_access_channels(websocketpp::log::alevel::none);

    wssrv.init_asio();
    wssrv.set_reuse_addr(true);

    wssrv.set_http_handler(std::bind(http_callback,&wssrv,std::placeholders::_1));
    wssrv.set_open_handler(std::bind(wsopen_callback,&wssrv,std::placeholders::_1));
    wssrv.set_close_handler(std::bind(wsclose_callback,&wssrv,std::placeholders::_1));
    wssrv.set_message_handler(std::bind(wsmsg_callback,&wssrv,std::placeholders::_1,std::placeholders::_2));

    wssrv.listen(9002);

    wssrv.start_accept();

    wssrv.run();
    return 0;
}